<?php
/**
 * Created by PhpStorm.
 * User: JYuans
 * Date: 2019/12/6
 * Time: 16:41
 */
namespace Bstage\Controller;

class TeamController extends BstageController {
    /**
     * 班子列表查询
     */
    public function teamQuery(){
        //分页
        $now_page = $_REQUEST['now_page']?$_REQUEST['now_page']:1;//默认第一页
        $page_size = $_REQUEST['page_size']?$_REQUEST['page_size']:50;//默认每页50条
        //是否获取全部
        $is_all = $_REQUEST["is_all"]?$_REQUEST["is_all"]:0;
        //查询条件
        $where = array();
        //筛选姓名
        if($_REQUEST["search_name"]){
            $where['name'] = ['like','%'.$_REQUEST['search_name'].'%'];
        }

        $field = "team_id,name,name_abb,add_time";
        if($is_all){
            $list_data = M('Team')
                ->where($where)
                ->field($field)
                ->order('add_time DESC')
                ->select();
        }else{
            $list_data = M('Team')
                ->where($where)
                ->field($field)
                ->order('add_time DESC')
                ->page($now_page,$page_size)
                ->select();
        }

        if(!$list_data) {
            $this->returnJson(503,'没有更多数据');
        }

        foreach($list_data as $key=>$value){
            $list_data[$key]['add_time'] = changeDateTime($value['add_time']);
        }

        $count = M('Team')
            ->where($where)
            ->count();

        $return_data = array();
        $return_data['list_data'] = $list_data;
        $return_data['count'] = $count;
        $return_data['now_page'] = $now_page;
        $return_data['page_size'] = $page_size;
        $return_data['max_page'] = ceil($count / $page_size);

        $this->successReturn("获取成功",$return_data);
    }

    /**
     * 添加接口
     */
    public function addTeamAction(){
        $new_data = array();
        $new_data['name'] = $_REQUEST['name']?$_REQUEST['name']:$this->errorReturn("请输入班子全称");

        $check_name = M("Team")
            ->where(['name'=>$new_data['name']])
            ->count();
        if($check_name){
            $this->errorReturn("该班子全称已存在");
        }

        $new_data['name_abb'] = $_REQUEST['name_abb']?$_REQUEST['name_abb']:$this->errorReturn("请输入班子简称");
        $check_name_abb = M("Team")
            ->where(['name_abb'=>$new_data['name_abb']])
            ->count();
        if($check_name_abb){
            $this->errorReturn("该班子简称已存在");
        }
        $new_data['add_time'] = time();

        $add_result = M("Team")
            ->add($new_data);

        if(!$add_result){
            $this->errorReturn("添加班子失败，请重新尝试");
        }

        $this->successReturn("添加成功");
    }

    /**
     * 修改信息接口
     */
    public function editTeamAction(){
        $team_id = $_REQUEST['team_id']?$_REQUEST['team_id']:$this->errorReturn("缺少班子ID");

        $new_data = array();
        $new_data['name'] = $_REQUEST['name']?$_REQUEST['name']:$this->errorReturn("请输入班子全称");
        $check_name = M("Team")
            ->where(['name'=>$new_data['name'],'team_id'=>['neq',$team_id]])
            ->count();
        if($check_name){
            $this->errorReturn("该班子全称已存在");
        }

        $new_data['name_abb'] = $_REQUEST['name_abb']?$_REQUEST['name_abb']:$this->errorReturn("请输入班子简称");
        $check_name_abb = M("Team")
            ->where(['name_abb'=>$new_data['name_abb'],'team_id'=>['neq',$team_id]])
            ->count();
        if($check_name_abb){
            $this->errorReturn("该班子简称已存在");
        }

        $new_data['update_time'] = time();

        $update_result = M("Team")
            ->where(['team_id'=>$team_id])
            ->save($new_data);

        if(!$update_result){
            $this->errorReturn("更新班子信息失败，请重新尝试");
        }

        $this->successReturn("修改成功");
    }

    /**
     * 删除接口
     */
    public function deleteTeamAction(){
        $team_id = $_REQUEST['id_str'] ? $_REQUEST['id_str'] : $this->errorReturn("缺少班子ID");

        $delete_result = M("Team")
            ->where(['team_id'=>['in',$team_id]])
            ->delete();

        if(!$delete_result){
            $this->errorReturn("删除班子失败，请重新尝试");
        }

        M("TeamExamineRelationDown")
            ->where(['be_examine_team_id'=>['in',$team_id],'comprehensive_status'=>0])
            ->delete();

        M("TeamExamineRelationSame")
            ->where(['be_examine_team_id'=>['in',$team_id],'comprehensive_status'=>0])
            ->delete();

        M("TeamExamineRelationUp")
            ->where(['be_examine_team_id'=>['in',$team_id],'comprehensive_status'=>0])
            ->delete();

        $this->successReturn("删除成功");
    }
}